<?php
/**
 * Created by PhpStorm.
 * User: dyane
 * Date: 2018/11/22
 * Time: 17:42
 */

namespace app\admin\controller;
use think\Db;
class Excel extends Base
{
    public function importFile(){
        $file = $_FILES['upload']['tmp_name'];
        if(!$file){
            echo '<script>alert("请先选择文件再导入")</script>';
            return view('user/index');
        }
        $fileTypes = explode(".", $_FILES ['upload'] ['name']);
    	$fileType = $fileTypes [count($fileTypes) - 1];
        $data = $this->importExcel($file,$fileType);
        foreach ($data as $key => $value) {
        	$info[$key]['name'] = $value[0];
        	$info[$key]['phone'] = $value[1];
        	$info[$key]['age'] = $value[2];
        	$info[$key]['sex'] = $value[3] == '女'?2:1;
        	$info[$key]['is_social'] = $value[4] == '是'?1:2;
        	$info[$key]['social_base'] = $value[5];
        	$info[$key]['social_area'] = $value[6];
        	$info[$key]['is_stay'] = $value[7] == '是'?1:0;
        	$info[$key]['leave_time'] = $value[8].' 00:00:00';
        	$info[$key]['leave_result'] = $value[9];
        	if($value[10] == '邀约中'){
        		$info[$key]['status'] = 1;
        	}elseif($value[10] == '到面'){
        		$info[$key]['status'] = 2;
        	}elseif($value[10] == '通过'){
        		$info[$key]['status'] = 3;
        	}elseif ($value[10] == '未通过') {
        		$info[$key]['status'] = 4;
        	}elseif ($value[10] == '待入职') {
        		$info[$key]['status'] = 5;
        	}elseif ($value[10] == '入职') {
        		$info[$key]['status'] = 6;
        	}elseif ($value[10] == '在职') {
        		$info[$key]['status'] = 7;
        	}elseif ($value[10] == '离职') {
        		$info[$key]['status'] = 8;
        	}
        	$info[$key]['is_contract'] = $value[11] == '是'?1:0;
        	$info[$key]['source'] = $value[12];
        }
        // dump($info);die;
        $res = Db::name('user')->insertAll($info);
        if($res){
        	return view('user/index');
        }
    }
    private function importExcel($file,$fileType){
        // 判断文件是什么格式
        ini_set('max_execution_time', '0');
        Vendor('PHPExcel176.PHPExcel');
        // 判断使用哪种格式
        if (strtolower($fileType) == 'xls'){
        	//判断excel表类型为2003还是2007
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        } elseif (strtolower($fileType) == 'xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        }
        $objPHPExcel = $objReader->load($file);
        $sheet = $objPHPExcel->getSheet(0);
        // 取得总行数
        $highestRow = $sheet->getHighestRow();
        // 取得总列数
        $highestColumn = $sheet->getHighestColumn();
        //循环读取excel文件,读取一条,插入一条
        $data=array();
        //从第一行开始读取数据
        for($j=2;$j<=$highestRow;$j++){
            //从A列读取数据
            for($k='A';$k<=$highestColumn;$k++){
                // 读取单元格
                $data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getvalue();
            }
        }
        return $data;
    }
}